package cn.iocoder.yudao.module.erp.service.accountplan;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.erp.controller.admin.accountplan.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.accountplan.AccountPlanDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.accountplan.AccountPlanMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * 对账单对应计划数据 Service 实现类
 *
 * @author mhc
 */
@Service
@Validated
public class AccountPlanServiceImpl implements AccountPlanService {

    @Resource
    private AccountPlanMapper accountPlanMapper;

    @Override
    public Long createAccountPlan(AccountPlanSaveReqVO createReqVO) {
        // 插入
        AccountPlanDO accountPlan = BeanUtils.toBean(createReqVO, AccountPlanDO.class);
        accountPlanMapper.insert(accountPlan);
        // 返回
        return accountPlan.getId();
    }

    @Override
    public void updateAccountPlan(AccountPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateAccountPlanExists(updateReqVO.getId());
        // 更新
        AccountPlanDO updateObj = BeanUtils.toBean(updateReqVO, AccountPlanDO.class);
        accountPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteAccountPlan(Long id) {
        // 校验存在
        validateAccountPlanExists(id);
        // 删除
        accountPlanMapper.deleteById(id);
    }

    private void validateAccountPlanExists(Long id) {
        if (accountPlanMapper.selectById(id) == null) {
            throw exception(ACCOUNT_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public AccountPlanDO getAccountPlan(Long id) {
        return accountPlanMapper.selectById(id);
    }

    @Override
    public PageResult<AccountPlanDO> getAccountPlanPage(AccountPlanPageReqVO pageReqVO) {
        return accountPlanMapper.selectPage(pageReqVO);
    }

}